package com.cmsz.centralservice.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * description : 根据#{key}替换key的value值的工具类
 */
public class KeyReplaceUtil {
    /**
     * 表达式前缀
     */
    public static final String prefix = "#\\{";
    /**
     * 表达式后缀
     */
    public static final String suffix = "\\}";

    /**
     * 替换#{key}表达式中字符串
     *
     * @param str   带替换的字符串
     * @param key   表达式#{key}中的key
     * @param value 对应key的值
     * @return 返回替换后新的字符串
     */
    public static String keyReplace(String str, String key, String value) {
        Pattern pattern = Pattern.compile(prefix + key + suffix);
        Matcher matcher = pattern.matcher(str);
        return matcher.replaceAll(value);
    }

    /**
     * 替换#{key}表达式中字符串
     *
     * @param str   带替换的字符串
     * @param entry 包含多个需要替换的key,value
     * @return 返回替换后新的字符串
     */
    public static String keyReplace(String str, Map<String, String> entry) {
        for (Map.Entry<String, String> keyValue : entry.entrySet()) {
            str = keyReplace(str, keyValue.getKey(), keyValue.getValue());
        }
        return str;
    }

    /**
     * 读取文件并替换文件中#{key}表达式中字符串
     *
     * @param pathname    需要替换的文件全路径
     * @param outPathname 替换后新生成输出的文件全路径
     * @param key         表达式#{key}中的key
     * @param value       对应key的值
     * @throws IOException 文件读写异常
     */
    public static void keyReplace(String pathname, String outPathname, String key, String value) throws IOException {
        if(!checkPathExists(outPathname)){
            return;
        }
        try (BufferedReader br = new BufferedReader(new FileReader(new File(pathname)));
             BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outPathname)))) {
            String line;
            while ((line = br.readLine()) != null) {
                String l = keyReplace(line, key, value);
                bw.write(l);
                bw.newLine();
            }
        }
    }

    private static boolean checkPathExists(String outPathname) {
        File out = new File(outPathname.substring(0,outPathname.lastIndexOf(File.separator)));
        if (!out.exists()){
            return out.mkdirs();
        }
        return true;
    }

    /**
     * 读取文件并替换文件中#{key}表达式中字符串
     *
     * @param pathname    需要替换的文件全路径
     * @param outPathname 替换后新生成输出的文件全路径
     * @param entry       包含多个需要替换的key,value
     * @throws IOException 文件读写异常
     */
    public static void keyReplace(String pathname, String outPathname, Map<String, String> entry) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader(new File(pathname)));
             BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outPathname)))) {
            String line;
            while ((line = br.readLine()) != null) {
                String l = keyReplace(line, entry);
                bw.write(l);
                bw.newLine();
            }
        }
    }

}
